<?php
namespace Admin\Controller;

use Admin\Controller\BaseController;

/**
 * 设置模块
 *
 * @author ryan
 *        
 */
class SettingsController extends BaseController
{

    /**
     * 菜单管理
     */
    public function menu()
    {
        if (IS_POST) {
            $pid = I('pid');
            $page = I('page');
            $limit = I('limit');
            $condition = array();
            $condition['pid'] = $pid;
            $list = D('Menu')->where($condition)
                ->order('sort_order asc')
                ->page($page, $limit)
                ->select();
            $count = D('Menu')->where($condition)->count();
            $result = array(
                'code' => 0,
                'msg' => $msg,
                'count' => $count,
                'data' => $list
            );
            $this->ajaxReturn($result);
        }
        $this->display();
    }

    /**
     * 添加菜单
     */
    public function addMenu()
    {
        if (IS_POST) {
            $info = I('info');
            if (empty($info['pid'])) {
                $info['pid'] = 0;
            } else {
                $menu = D('Menu')->where(array(
                    'id' => $info['pid']
                ))->find();
                if (empty($menu)) {
                    $this->error('上级菜单不存在');
                }
            }
            $info['level'] = $menu['level'] + 1;
            $info['status'] = 0;
            $info['add_time'] = TIMESTAMP;
            $status = D('Menu')->add($info);
            if ($status === false) {
                $this->error('添加失败');
            } else {
                $this->success('添加成功');
            }
        }
        $menus = D('Menu')->menus(array(
            'pid' => 0
        ), 2);
        $this->assign('menus', $menus);
        $this->display();
    }

    /**
     * 修改菜单
     */
    public function editMenu()
    {
        $nid = I('nid');
        if (empty($nid)) {
            $this->error('异常操作');
        }
        $menu = D('Menu')->where(array(
            'id' => $nid
        ))->find();
        if (empty($menu)) {
            $this->error('数据异常');
        }
        if (IS_POST) {
            $info = I('info');
            if (empty($info['pid'])) {
                $info['pid'] = 0;
                $info['level'] = 1;
            } else {
                $p_menu = D('Menu')->where(array(
                    'id' => $info['pid']
                ))->find();
                $info['level'] = $p_menu['level'] + 1;
            }
            $status = D('Menu')->where(array(
                'id' => $nid
            ))->save($info);
            if ($status === false) {
                $this->error('修改失败');
            } else {
                $this->success('修改成功');
            }
        }
        $menus = D('Menu')->menus(array(
            'pid' => 0
        ), 2);
        $this->assign('menus', $menus);
        $this->assign('obj', $menu);
        $this->display();
    }

    /**
     * 删除菜单
     */
    public function delMenu()
    {}

    public function ajaxMenus()
    {
        $token = I('token');
        $list = array();
        if (empty($token)) {
            $this->ajaxReturn($list);
        }
        $user_token = D('UserToken')->where(array(
            'token' => $token,
            'status' => 0
        ))->find();
        if (empty($user_token)) {
            $this->ajaxReturn($list);
        } else {
            $pid = I('pid');
            $menus = D('Menu')->menus(array(
                'pid' => $pid,
                'status' => 0
            ), 2);
            $list = $menus;
        }
        $this->ajaxReturn($list);
    }

    /**
     * 城市管理
     */
    public function regions()
    {
        if (IS_POST) {
            $method = I('method');
            if ($method === 'list') {
                $regions = $this->regionToTree();
                $this->ajaxReturn($regions);
            }
        }
        $this->display();
    }

    public function addRegion()
    {
        if (IS_POST) {
            $info = I('info');
            $info['add_time'] = TIMESTAMP;
            $pid = $info['pid'];
            if (empty($pid)) {
                $info['level'] = 1;
            } else {
                $region = D('Regions')->where(array(
                    'id' => $pid
                ))->find();
                $info['level'] = $region['level'] + 1;
            }
            $status = D('Regions')->add($info);
            if ($status === false) {
                $this->error('添加失败');
            } else {
                $this->success('添加成功');
            }
        }
        $pid = I('pid');
        if (! empty($pid)) {
            $region = D('Regions')->where(array(
                'id' => $pid
            ))->find();
        }
        $regions = $this->regionToTree();
        $this->assign('regions', $regions);
        $this->assign('region', $region);
        $this->display();
    }

    public function editRegion()
    {
        $nid = I('nid');
        if (empty($nid)) {
            $this->error('异常操作');
        }
        if (IS_POST) {
            $info = I('info');
            $status = D('Regions')->where(array(
                'id' => $nid
            ))->save($info);
            if ($status === false) {
                $this->error('修改失败');
            } else {
                $this->success('修改成功');
            }
        }
        $region = D('Regions')->where(array(
            'id' => $nid
        ))->find();
        if (empty($region)) {
            $this->error('数据异常');
        }
        $regions = $this->regionToTree();
        $this->assign('regions', $regions);
        $this->assign('region', $region);
        $this->display();
    }

    private function regionToTree($pid = 0, $level = 3)
    {
        $condition = array();
        $condition['pid'] = $pid;
        $condition['level'] = array(
            'ELT',
            $level
        );
        $regions = D('Regions')->where($condition)
            ->order('id asc')
            ->select();
        foreach ($regions as $key => $region) {
            $children = $this->regionToTree($region['id'], $level);
            if ($level > $region['level']) {
                $region['children'] = $children;
            }
            $regions[$key] = $region;
        }
        return $regions;
    }
}

